<!DOCTYPE html>
<!DOCTYPE html>
<html lang="zh-cmn-Hans">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,viewport-fit=cover">
    <title>扫码注册</title>
    <link rel="stylesheet" href="/static/css/weui.css" />
    <link rel="stylesheet" href="/static/css/example.css" />
    <style type="text/css">
        .center-in-center {
            position: absolute;
            top: 35%;
            left: 25%;
        }
        
        .center-in-top {
            position: absolute;
            top: 15%;
            left: 40%;
        }
        
        .center-in-bottom {
            position: absolute;
            top: 48%;
            left: 30%;
        }
    </style>
</head>

<body ontouchstart>
    <div class="container" id="container"></div>
    <script type="text/html" id="tpl_home">
        <div class="page">

            <!-- <div class="center-in-top"><label class="weui-label" style="font-size: 15px;color: rgba(0,0,0,0.5); white-space: pre-wrap;">扫码操作可能会因为网络情况反应较慢,请您耐心等待</label></div>

            <div class="center-in-bottom">
                <a class="weui-btn weui-btn_primary" href="javascript:" id="submit">扫码确认</a>
            </div>

            <div id="js_toast" style='opacity: 0; display: none;'>
                <div class="weui-mask_transparent"></div>
                <div class="weui-toast">
                    <i class="weui-icon-success-no-circle weui-icon_toast"></i>
                    <p class="weui-toast__content">已完成</p>
                </div> 
            </div> -->
        </div>


        <script src="/static/js/jquery.min.js"></script>
        <script src="/static/js/weui.min.js"></script>
        <script type="text/javascript" src="/static/js/jweixin-1.4.0.js"></script>
        <script src="/static/js/zepto.min.js"></script>
        <script src="/static/js/example.js"></script>



        <script type="text/javascript">
            $(function() {

                function getQueryStringCODE(name) {
                    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
                    var r = decodeURI(window.location.search).substr(1).match(reg);
                    if (r != null) return (r[2]);
                    return null;
                };


                $(document).ready(function() {
                    // 获取code
                    var code = getQueryStringCODE('code');

                    //需要把当前页面的url地址传到后台，生成签名信息时需要使用到
                    var tokenUrl = location.href.split('#')[0];
                    console.log("当前界面的网址为：");
                    console.log(tokenUrl);
                    //获取签名的后台接口
                    var _getWechatSignUrl = 'http://gzh.zjnnhj.com/niuneng/genSign';
                    //获取签名
                    $.ajax({
                        url: _getWechatSignUrl,
                        type: "post",
                        data: JSON.stringify({
                            "data": tokenUrl
                        }),
                        dataType: "json",
                        success: function(res) {
                            console.log(res);
                            //获得签名之后传入配置中进行配置
                            wxConfig(res.appId, res.timestamp, res.nonceStr, res.signature);

                        }
                    })

                    function wxConfig(_appId, _timestamp, _nonceStr, _signature) {
                        console.log('获取数据：' + _timestamp + '\n' + _nonceStr + '\n' + _signature);
                        //_timestamp = 1232;
                        //e4589803f20e294518c1bf39b137b8a5daa5fd3f randonstring wx8afd92044afa72dd 1573218266
                        wx.config({
                            debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                            appId: _appId, // 必填，公众号的唯一标识
                            timestamp: _timestamp, // 必填，生成签名的时间戳
                            nonceStr: _nonceStr, // 必填，生成签名的随机串
                            signature: _signature, // 必填，签名，见附录1
                            jsApiList: ['checkJsApi', 'scanQRCode']
                                // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
                        });
                    }

                    console.log('验证完毕，开始扫描')
                    var result = '';


                    wx.ready(function() {
                        wx.scanQRCode({
                            desc: 'scanQRCode desc',
                            needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
                            scanType: ["qrCode"], // 可以指定扫二维码还是一维码，默认二者都有
                            success: function(res) {
                                //res = JSON.parse(res)
                                console.log("调用扫描成功,处理扫描结果", res);
                                //alert("调用扫描成功,处理扫描结果");
                                result = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
                                // 切分出设备id，设备编号
                                if (result.indexOf(';') != -1){
                                    var commuNum = result.substr(0, result.indexOf(';'));
                                    var device = result.substr(result.indexOf(';') + 1);
                                    // result 进行截取处理有分号的话
                                    console.log(result);
                                    var fredirect_uri = 'http://gzh.zjnnhj.com/niuneng/register.html?device=' + device + '&community=' + commuNum;
                                    var ffurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx5f2850581e4e064d&redirect_uri=" + encodeURIComponent(fredirect_uri) + "&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
                                    location.href = ffurl;
                                } else{
                                    alert('请扫描有效二维码')
                                    setTimeout(function() {
                                        //这个可以关闭安卓系统的手机
                                        document.addEventListener(
                                            "WeixinJSBridgeReady",
                                            function() {
                                                WeixinJSBridge.call("closeWindow");
                                            },
                                            false
                                        );
                                            //这个可以关闭ios系统的手机
                                        WeixinJSBridge.call("closeWindow");
                                        }, 500);

                                }
                            }

                        });
                    });
                });
            });
        </script>

    </script>
</body>

</html>